package com.zr.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.AuthDao;
import com.zr.model.Auth;
import com.zr.util.JDBCUtil;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class AuthDaoImpl implements AuthDao{

	@Override
	public JSONArray getAuthsByPid(int pid) {
		// TODO Auto-generated method stub
	    JSONArray   as = new JSONArray();
		StringBuffer  sql = new StringBuffer();
		sql.append("  select aid,aname,astate,aurl from z_auth");
		sql.append( " where  parentId=?");
		Connection  con = JDBCUtil.getConnection();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1,pid);
			ResultSet  rs = pst.executeQuery();
			while(rs.next()){
				JSONObject j = new JSONObject();
				j.put("id", rs.getInt("aid"));
				j.put("text",rs.getString("aname"));
				j.put("state",rs.getString("astate"));
				JSONObject  attr = new JSONObject();
				attr.put("url", rs.getString("aurl"));
				j.put("attributes", attr);
				as.add(j);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return as;
	}
		
}
